<?php
/**
 * Nad�b (Mak�-Nad�b)
 *
 * @filesource 
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license	Freeware
 * @package	Nadeb_Controller
 * @subpackage Nadeb.Controller.Crud
 * @version	2.0
 */

class Nadeb_Controller_Crud extends Nadeb_Controller_Front
{
	private $module;
	private $controller;
	private $modelName;
	
	public function init()
	{
		$js               = Nadeb_JScontroller::get_instance();
		$auth             = Nadeb_Login::get_instance();
		$this->module     = $this->_getParam("module");
		$this->controller = $this->_getParam("controller");
		$this->modelName  = ucfirst( $this->module ) . "_Models_" . ucfirst( $this->controller );
		$js->JSInstance   = "admin";
		
		if(!$auth->hasIdentity())
		{
			$this->_redirect("/$this->module/");
		}
		else
		{
			if( $auth->get_info()->use_level == 'master')
			{
				$js->JSInstance = "admin_shortCut";
			}
			
			parent::init();
		}
	}

	public function indexAction()
	{
		$this->_redirect("/$this->module/$this->controller/grid/");
	}

	public function gridAction()
	{
		$session = new Zend_Session_Namespace();
		$session->referer = $_SERVER['REDIRECT_URL'];
		
		$js             = Nadeb_JScontroller::get_instance();
		$js->JSInstance = "admin_grid";
		
		$this->view->grid = Nadeb_Grid::get_grid( $this->_getAllParams() );
		$this->renderScript("crud/grid.phtml");
	}
	
	public function filterAction()
	{
		$tp                = $this->_getParam('type');
		$sqlFilters        = new Zend_Session_Namespace();
		$sqlFilters->types = str_replace($tp . ',','',$sqlFilters->types) . $tp . ","; 
		
		call_user_func( array( "Nadeb_Report_Filters_" . ucfirst( $tp ), "save_sql" ), $this->_getAllParams() );
		
		$session = new Zend_Session_Namespace();
		$this->_redirect( $session->referer );
	}
	
	public function filtercancelAction()
	{
		$tp                = $this->_getParam('type');
		$sqlFilters        = new Zend_Session_Namespace();
		$sqlFilters->types = str_replace($tp . ',','',$sqlFilters->types); 
		
		call_user_func( array( "Nadeb_Report_Filters_" . ucfirst( $this->_getParam('type') ), "cancel_sql" ) );
		
		$session = new Zend_Session_Namespace();
		$this->_redirect( $session->referer );
	}

	public function formAction()
	{
		$js             = Nadeb_JScontroller::get_instance();
		$js->JSInstance = "admin_form";
		
		$this->view->form = Nadeb_Form::get_form( $this->_getAllParams() );
		$this->renderScript("crud/form.phtml");
	}

	public function insertAction()
	{
		$insert = new Nadeb_Data_Insert( "$this->module","$this->controller",$this->_request->getPost() );
		$this->view->response = $insert->get_result();
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}

	public function updateAction()
	{
		$update = new Nadeb_Data_Update( "$this->module","$this->controller",$this->_request->getPost() );
		$this->view->response = $update->get_result();
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function insertGetIdAction()
	{
		$insert = new Nadeb_Data_Insert( "$this->module","$this->controller",$this->_request->getPost() );
		$this->view->response = $insert->get_insertID();
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function updateGetIdAction()
	{
		$update = new Nadeb_Data_Update( "$this->module","$this->controller",$this->_request->getPost() );
		$this->view->response = $update->get_insertID();
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}

	public function deleteAction()
	{
		$delete = new Nadeb_Data_Delete( "$this->module","$this->controller",$this->_request->getPost() );
		$this->view->response = $delete->get_result();
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}

	public function swapAction()
	{
		$modelo = new $this->modelName();
		$modelo->swap_active( $this->_getParam("id") );
		$this->view->response = $modelo->get_result();
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function saveorderAction()
	{
		$modelo = new $this->modelName();
		$this->view->response = $modelo->save_order( $this->_getParam("neworder") );
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function listFolderAction()
	{
		$modelo = new $this->modelName();
		$this->view->response = $modelo->list_galery_json( $this->_getParam("folder") );
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function addToFolderAction()
	{
		$modelo = new $this->modelName();
		$this->view->response = $modelo->add_to_folder( $this->_getParam("folder") );
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function delToFolderAction()
	{
		$modelo = new $this->modelName();
		$this->view->response = $modelo->del_to_folder( $this->_request->getParams() );
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function saveLegendAction()
	{
		$modelo = new $this->modelName();
		$this->view->response = $modelo->save_galery_json( $this->_getParam("folder") );
		
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}
	
	public function crudAction()
	{
		if( !$this->_getParam("modelo") )
			$this->view->response = "digite um modelo (EX: /inicial/crud/modelo/nome_da_tabela )";
		else
		{
			$variables = Nadeb_Controller_Vars::get_variables( $this->_getParam("module"),$this->_getParam("modelo"),'tb_','_id' );
			Nadeb_Controller_Models::save( $variables );
			Nadeb_Controller_Controllers::save( $variables );
			Nadeb_Controller_Configs::save( $variables );
			
			$this->view->response = "OK <br />Ver como ficou: <a href='/admin/{$this->_getParam("modelo")}/'>{$this->_getParam("modelo")}</a>";
		
		}
		$this->_helper->layout->disableLayout();
		$this->renderScript("ajax/response.phtml");
	}

	public static function get_name($_param)
	{
		$arr  = explode("-",$_param);
		$name = "";
		
		for($i=0;$i<count($arr);$i++)
			$name .= ucfirst($arr[$i]);
		
		return $name;
	}
}